//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.geotools.data.mysql;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
import java.util.Map;
import org.geotools.data.DataAccessFactory;
import org.geotools.jdbc.JDBCDataStore;
import org.geotools.jdbc.JDBCDataStoreFactory;
import org.geotools.jdbc.SQLDialect;

public class MySQLDataStoreFactory extends JDBCDataStoreFactory {
    public static final DataAccessFactory.Param DBTYPE = new DataAccessFactory.Param("dbtype", String.class, "Type", true, "mysql", Collections.singletonMap("level", "program"));
    public static final DataAccessFactory.Param PORT = new DataAccessFactory.Param("port", Integer.class, "Port", true, 3306);
    public static final DataAccessFactory.Param STORAGE_ENGINE = new DataAccessFactory.Param("storage engine", String.class, "Storage Engine", false, "MyISAM");
    public static final DataAccessFactory.Param ENHANCED_SPATIAL_SUPPORT = new DataAccessFactory.Param("enhancedSpatialSupport", Boolean.class, "Enhanced Spatial Support", false, false);
    protected boolean enhancedSpatialSupport;

    public MySQLDataStoreFactory() {
        this.enhancedSpatialSupport = (Boolean)ENHANCED_SPATIAL_SUPPORT.sample;
    }

    protected SQLDialect createSQLDialect(JDBCDataStore dataStore) {
        return new MySQLDialectBasic(dataStore, this.enhancedSpatialSupport);
    }

    public String getDisplayName() {
        return "MySQL";
    }

    protected String getDriverClassName() {
        return "com.mysql.cj.jdbc.Driver";
    }

    protected String getDatabaseID() {
        return (String)DBTYPE.sample;
    }

    public String getDescription() {
        return "MySQL Database";
    }

    protected String getValidationQuery() {
        return "select version()";
    }

    protected void setupParameters(Map parameters) {
        super.setupParameters(parameters);
        parameters.put(DBTYPE.key, DBTYPE);
        parameters.put(PORT.key, PORT);
        parameters.put(STORAGE_ENGINE.key, STORAGE_ENGINE);
        parameters.remove(SCHEMA.key);
    }

    protected JDBCDataStore createDataStoreInternal(JDBCDataStore dataStore, Map params) throws IOException {
        String storageEngine = (String)STORAGE_ENGINE.lookUp(params);
        if (storageEngine == null) {
            storageEngine = (String)STORAGE_ENGINE.sample;
        }

        Boolean enhancedSpatialFlag = (Boolean)ENHANCED_SPATIAL_SUPPORT.lookUp(params);
        if (enhancedSpatialFlag == null) {
            this.enhancedSpatialSupport = isMySqlVersion56(dataStore);
        } else if (enhancedSpatialFlag && !isMySqlVersion56(dataStore)) {
            dataStore.getLogger().info("MySQL version does not support enhancedSpatialSupport. Disabling it");
            this.enhancedSpatialSupport = false;
        }

        SQLDialect dialect = dataStore.getSQLDialect();
        if (dialect instanceof MySQLDialectBasic) {
            ((MySQLDialectBasic)dialect).setStorageEngine(storageEngine);
            ((MySQLDialectBasic)dialect).setUsePreciseSpatialOps(this.enhancedSpatialSupport);
        } else {
            ((MySQLDialectPrepared)dialect).setStorageEngine(storageEngine);
            ((MySQLDialectPrepared)dialect).setUsePreciseSpatialOps(this.enhancedSpatialSupport);
        }

        return dataStore;
    }

    protected static boolean isMySqlVersion56(JDBCDataStore dataStore) {
        boolean isMySQLVersion56OrAbove = false;
        Connection con = null;

        try {
            con = dataStore.getDataSource().getConnection();
            int major = con.getMetaData().getDatabaseMajorVersion();
            int minor = con.getMetaData().getDatabaseMinorVersion();
            if (major > 5 || major == 5 && minor > 6) {
                isMySQLVersion56OrAbove = true;
            } else {
                isMySQLVersion56OrAbove = false;
            }
        } catch (Exception var13) {
            var13.printStackTrace();
        } finally {
            try {
                if (con != null || !con.isClosed()) {
                    con.close();
                }
            } catch (SQLException var12) {
            }

        }

        return isMySQLVersion56OrAbove;
    }

    protected String getJDBCUrl(Map params) throws IOException {
        String url = super.getJDBCUrl(params);
        url = url + "?characterEncoding=utf8";
        return url;
    }
}
